/*
Designer   : Renyangang

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package device

import (
	"fpga-vboard/fpga"
	"fpga-vboard/logger"
	"fpga-vboard/protocol"
)

const (
	SOC_GLOBAL = 1
	CPU        = 2
	RTC        = 3
	FLASH      = 4
	SDRAM      = 5
	GPIO       = 6
	UartDiaply = 7
	Switchs    = 8
	Leds       = 9
	NixieTube  = 10
)

var DEV_TYPE_NAMES = []string{"unkown", "soc_global", "cpu", "rtc", "flash", "sdram", "gpio", "serial", "switchs", "leds", "nixie_tube"}

const (
	SOC_GLOBAL_DEFAULT = 1
	FLASH_DEFAULT      = 2
	SDRAM_DEFAULT      = 3
	GPIO_DEFAULT       = 4
	RTC_DEFAULT        = 5
	CPU_DEFAULT        = 6
	UartDiaply_DEFAULT = 7
	Switchs_DEFAULT    = 8
	Leds_DEFAULT       = 9
	NixieTube_DEFAULT  = 10
)

// var DEV_NAMES = []string{"unkown", "default_soc_global", "default_flash", "default_sdram", "default_gpio", "default_rtc", "default_cpu", "serial_default"}

var devMap = map[int]any{}
var devTypeMap = map[int][]int{}

func GetDev(id int) any {
	return devMap[id]
}

func GetDevsByType(devType int) []any {
	devIds := devTypeMap[devType]
	if devIds == nil {
		return nil
	}
	devs := make([]any, len(devIds))
	for i, devId := range devIds {
		devs[i] = devMap[devId]
	}
	return devs
}

func RegisterDev(dev DeviceConfig) {
	dev.GenInfo()
	if devMap[dev.Info().Id] != nil {
		logger.FatalEvent("device id %d already exist", dev.Info().Id)
		return
	}
	devMap[dev.Info().Id] = dev
	if devTypeMap[dev.Info().Type] == nil {
		devTypeMap[dev.Info().Type] = []int{}
	}
	devTypeMap[dev.Info().Type] = append(devTypeMap[dev.Info().Type], dev.Info().Id)
}

type RamConfig struct {
	DeviceInfo
}

type FlashConfig struct {
	DeviceInfo
	Size     int
	FilePath string `label:"bin file path" required:"true" type:"fileInput" filters:"*.bin"`
}

type RealTimerConfig struct {
	DeviceInfo
	TimerClockFrequency int64 `label:"timer clock frequency" required:"true" type:"clock" min:"1"`
}

type UartDisplayConfig struct {
	DeviceInfo
	UartConfig   *protocol.UARTConfig `type:"subconfig"`
	ShowTerminal bool                 `label:"show terminal" required:"true" type:"onoff"`
}

type CPUConfig struct {
	DeviceInfo
	VerilatorConfig *fpga.VerilatorConfig `type:"subconfig"`
	LibPath         string
}

type SocGlobalConfig struct {
	DeviceInfo
	ClockFrequency           uint64 `label:"main clock frequency" required:"true" type:"clock" min:"1"`
	ResetRemainingClockTimes int
}

type SwitchsConfig struct {
	DeviceInfo
	SwitchNumbers int
}

type LedsConfig struct {
	DeviceInfo
	LedNumbers  int
	Color       string `label:"Led Color" required:"false" type:"ledcolor"`
	LighingMode string `label:"Led Lighing Mode" required:"true" type:"select" options:"high,low"`
}

type NixieTubeConfig struct {
	DeviceInfo
	IICConfig *protocol.IICConfig `type:"subconfig"`
}
